home *** CD-ROM | disk | FTP | other *** search
/ Champak 140 / (Vol 140) Sep 19 2011.iso / Games / world-wars.swf / scripts / __Packages / classes / GameWW.as < prev    next >
Text File  |  2011-09-19  |  16KB  |  555 lines

  1. class classes.GameWW
  2. {
  3.    function GameWW(gmc, umc, etmc, wt, ul, fb)
  4.    {
  5.       this.GAME_MC = gmc;
  6.       this.UNITS_MC = umc;
  7.       this.WAITER = wt;
  8.       this.UNITS_LIST = ul;
  9.       this.END_TURN_MC = etmc;
  10.       this.FIGHTBOX = fb;
  11.       this.AUTOPLAYER = false;
  12.       this.MAP = new classes.Map(this.GAME_MC,this);
  13.       this.AI = new classes.GameAI(this);
  14.       this.counter = 0;
  15.       this.TRACER = _root.createEmptyMovieClip("tracer",1);
  16.       this.PLAYER_MOVE = 1;
  17.    }
  18.    function newGame(players, callBack)
  19.    {
  20.       if(this.WAITER.status == "ready_off")
  21.       {
  22.          var root = this;
  23.          this.PLAYERS_NO = players;
  24.          this.PLAYERS_CACHES = new Array();
  25.          var i = 0;
  26.          while(i < this.PLAYERS_NO)
  27.          {
  28.             this.PLAYERS_CACHES.push(0);
  29.             i++;
  30.          }
  31.          this.WAITER.show();
  32.          this.GAME_MC.onEnterFrame = function()
  33.          {
  34.             with(root)
  35.             {
  36.                if(WAITER.status == "ready_on")
  37.                {
  38.                   MAP.createMap(PLAYERS_NO);
  39.                   counter = MAP.TERRITORIES_ARR.length + 10;
  40.                   arrangeUnits();
  41.                   WAITER.hide();
  42.                   var refArr = new Array();
  43.                   var i = 1;
  44.                   while(i <= PLAYERS_NO)
  45.                   {
  46.                      refArr.push(unitsToAdd(i));
  47.                      i++;
  48.                   }
  49.                   UNITS_LIST.setList(MAP.TERRITORIES_ARR,refArr,PLAYERS_NO);
  50.                }
  51.             }
  52.             if(root.WAITER.status == "ready_off")
  53.             {
  54.                callBack();
  55.                delete this.onEnterFrame;
  56.             }
  57.          };
  58.       }
  59.    }
  60.    function init()
  61.    {
  62.       this.UNITS_LIST.selectPlayer(this.PLAYER_MOVE);
  63.       if(this.PLAYER_MOVE != 1 || this.AUTOPLAYER)
  64.       {
  65.          this.AI.cleverMove(this.PLAYER_MOVE);
  66.       }
  67.       else
  68.       {
  69.          this.END_TURN_MC.gotoAndPlay(2);
  70.          this.MAP.startInterface();
  71.       }
  72.    }
  73.    function nextPlayer()
  74.    {
  75.       this.PLAYER_MOVE = this.PLAYER_MOVE + 1;
  76.       if(this.PLAYER_MOVE > this.PLAYERS_NO)
  77.       {
  78.          this.PLAYER_MOVE = 1;
  79.       }
  80.       if(this.PLAYER_MOVE != 1 || this.AUTOPLAYER)
  81.       {
  82.          this.AI.cleverMove(this.PLAYER_MOVE);
  83.       }
  84.       else
  85.       {
  86.          this.END_TURN_MC.gotoAndPlay(2);
  87.          this.MAP.startInterface();
  88.       }
  89.       if(this.PLAYER_MOVE == 1)
  90.       {
  91.          this.FIGHTBOX.resetSkipMode();
  92.       }
  93.       this.UNITS_LIST.selectPlayer(this.PLAYER_MOVE);
  94.    }
  95.    function isGameFinished()
  96.    {
  97.       var _loc3_ = 0;
  98.       var _loc2_ = 0;
  99.       while(_loc2_ < this.MAP.TERRITORIES_ARR.length)
  100.       {
  101.          if(this.MAP.TERRITORIES_ARR[_loc2_].player == 1)
  102.          {
  103.             _loc3_ = _loc3_ + 1;
  104.          }
  105.          _loc2_ = _loc2_ + 1;
  106.       }
  107.       if(_loc3_ == this.MAP.TERRITORIES_ARR.length)
  108.       {
  109.          this.PLAYER_WIN();
  110.          return true;
  111.       }
  112.       if(_loc3_ == 0)
  113.       {
  114.          this.GAME_OVER();
  115.          return true;
  116.       }
  117.       return false;
  118.    }
  119.    function PLAYER_WIN()
  120.    {
  121.       _root.YOUWIN_MC._visible = true;
  122.       _root.YOUWIN_MC.gotoAndPlay(2);
  123.    }
  124.    function GAME_OVER()
  125.    {
  126.       _root.GAMEOVER_MC._visible = true;
  127.       _root.GAMEOVER_MC.gotoAndPlay(2);
  128.    }
  129.    function arrangeUnits()
  130.    {
  131.       var _loc9_ = [0,0,0,0,0,0,0,0];
  132.       var _loc8_ = new Array();
  133.       this.clearAll();
  134.       this.rubbish_arr = new Array();
  135.       var _loc3_ = 0;
  136.       while(_loc3_ < this.MAP.TERRITORIES_ARR.length)
  137.       {
  138.          var _loc4_ = this.MAP.TERRITORIES_ARR[_loc3_].arr;
  139.          var _loc7_ = 0;
  140.          var _loc6_ = 0;
  141.          var _loc2_ = 0;
  142.          while(_loc2_ < _loc4_.length)
  143.          {
  144.             _loc7_ += _loc4_[_loc2_]._x;
  145.             _loc6_ += _loc4_[_loc2_]._y;
  146.             _loc2_ = _loc2_ + 1;
  147.          }
  148.          _loc7_ /= _loc4_.length;
  149.          _loc6_ /= _loc4_.length;
  150.          this.counter = _loc3_;
  151.          var _loc5_ = this.UNITS_MC.attachMovie("UNIT","unit_" + this.counter,2 + this.counter * 2);
  152.          this.counter = this.counter + 1;
  153.          _loc5_._x = _loc7_;
  154.          _loc5_._y = _loc6_;
  155.          _loc5_.setUnit(this.MAP.TERRITORIES_ARR[_loc3_].army,this.MAP.TERRITORIES_ARR[_loc3_].unitsNO,this.MAP.TERRITORIES_ARR[_loc3_].player);
  156.          this.MAP.TERRITORIES_ARR[_loc3_].unitsMC = _loc5_;
  157.          _loc8_.push(_loc5_);
  158.          this.rubbish_arr.push(_loc5_);
  159.          _loc3_ = _loc3_ + 1;
  160.       }
  161.       this.unitsSmartDepths(_loc8_);
  162.    }
  163.    function unitsSmartDepths(unitsArr)
  164.    {
  165.       var _loc5_ = 0;
  166.       while(_loc5_ < unitsArr.length)
  167.       {
  168.          var _loc2_ = unitsArr.length - 1;
  169.          while(_loc2_ > _loc5_)
  170.          {
  171.             if(unitsArr[_loc2_ - 1]._y > unitsArr[_loc2_]._y)
  172.             {
  173.                var _loc4_ = unitsArr[_loc2_];
  174.                unitsArr[_loc2_] = unitsArr[_loc2_ - 1];
  175.                unitsArr[_loc2_ - 1] = _loc4_;
  176.             }
  177.             _loc2_ = _loc2_ - 1;
  178.          }
  179.          _loc5_ = _loc5_ + 1;
  180.       }
  181.       _loc5_ = 0;
  182.       while(_loc5_ < unitsArr.length)
  183.       {
  184.          unitsArr[_loc5_].swapDepths(2 + this.counter * 2);
  185.          this.counter = this.counter + 1;
  186.          _loc5_ = _loc5_ + 1;
  187.       }
  188.    }
  189.    function showBiggestArea()
  190.    {
  191.       var _loc7_ = new Array();
  192.       var _loc3_ = 0;
  193.       while(_loc3_ < this.MAP.TERRITORIES_ARR.length)
  194.       {
  195.          if(this.MAP.TERRITORIES_ARR[_loc3_].player == this.PLAYER_MOVE)
  196.          {
  197.             _loc7_.push(this.MAP.TERRITORIES_ARR[_loc3_]);
  198.          }
  199.          _loc3_ = _loc3_ + 1;
  200.       }
  201.       var _loc4_ = new Array();
  202.       _loc3_ = 0;
  203.       while(_loc3_ < _loc7_.length)
  204.       {
  205.          var _loc6_ = _loc7_[_loc3_];
  206.          if(this.isThere(_loc6_,_loc4_) == false)
  207.          {
  208.             var _loc5_ = new Array();
  209.             this.generate(_loc6_,_loc5_);
  210.             _loc4_.push(_loc5_);
  211.          }
  212.          _loc3_ = _loc3_ + 1;
  213.       }
  214.       this.maxArea = new Array();
  215.       _loc3_ = 0;
  216.       while(_loc3_ < _loc4_.length)
  217.       {
  218.          if(_loc4_[_loc3_].length > this.maxArea.length)
  219.          {
  220.             this.maxArea = _loc4_[_loc3_];
  221.          }
  222.          _loc3_ = _loc3_ + 1;
  223.       }
  224.       _loc3_ = 0;
  225.       while(_loc3_ < this.maxArea.length)
  226.       {
  227.          this.MAP.lightOnTerritory(this.maxArea[_loc3_]);
  228.          _loc3_ = _loc3_ + 1;
  229.       }
  230.       var _loc9_ = 0;
  231.       var _loc8_ = 0;
  232.       _loc3_ = 0;
  233.       while(_loc3_ < this.maxArea.length)
  234.       {
  235.          _loc9_ += this.maxArea[_loc3_].unitsMC._x;
  236.          _loc8_ += this.maxArea[_loc3_].unitsMC._y;
  237.          _loc3_ = _loc3_ + 1;
  238.       }
  239.       _loc9_ /= this.maxArea.length;
  240.       _loc8_ /= this.maxArea.length;
  241.       var _loc10_ = this.UNITS_MC.attachMovie("CLOUD","CLOUD",this.UNITS_MC.getNextHighestDepth());
  242.       _loc10_.toAdd = this.maxArea.length;
  243.       _loc10_._x = _loc9_;
  244.       _loc10_._y = _loc8_;
  245.       _global.setTimeout(mx.utils.Delegate.create(this,this.hideBiggestArea),1200);
  246.    }
  247.    function hideBiggestArea()
  248.    {
  249.       var _loc2_ = 0;
  250.       while(_loc2_ < this.maxArea.length)
  251.       {
  252.          this.MAP.lightOffTerritory(this.maxArea[_loc2_]);
  253.          _loc2_ = _loc2_ + 1;
  254.       }
  255.    }
  256.    function addUnits()
  257.    {
  258.       var _loc12_ = this.unitsToAdd(this.PLAYER_MOVE) + this.PLAYERS_CACHES[this.PLAYER_MOVE - 1];
  259.       var _loc5_ = new Array();
  260.       var _loc6_ = 0;
  261.       while(_loc6_ < this.MAP.TERRITORIES_ARR.length)
  262.       {
  263.          if(this.MAP.TERRITORIES_ARR[_loc6_].player == this.PLAYER_MOVE)
  264.          {
  265.             _loc5_.push(this.MAP.TERRITORIES_ARR[_loc6_]);
  266.          }
  267.          _loc6_ = _loc6_ + 1;
  268.       }
  269.       var _loc4_ = new Array();
  270.       _loc6_ = 0;
  271.       while(_loc6_ < _loc5_.length)
  272.       {
  273.          _loc4_.push(0);
  274.          _loc6_ = _loc6_ + 1;
  275.       }
  276.       var _loc11_ = _loc5_.length;
  277.       while(_loc12_ > 0 && _loc11_ > 0)
  278.       {
  279.          _loc11_ = _loc5_.length;
  280.          _loc6_ = 0;
  281.          while(_loc6_ < _loc4_.length)
  282.          {
  283.             if(_loc4_[_loc6_] + _loc5_[_loc6_].unitsNO >= 8)
  284.             {
  285.                _loc11_ = _loc11_ - 1;
  286.             }
  287.             _loc6_ = _loc6_ + 1;
  288.          }
  289.          var _loc7_ = this.random2(_loc5_.length - 1);
  290.          var _loc9_ = false;
  291.          _loc6_ = 0;
  292.          while(_loc6_ < _loc4_.length && !_loc9_)
  293.          {
  294.             if(_loc4_[(_loc7_ + _loc6_) % _loc4_.length] + _loc5_[(_loc7_ + _loc6_) % _loc4_.length].unitsNO < 8)
  295.             {
  296.                _loc4_[(_loc7_ + _loc6_) % _loc4_.length]++;
  297.                _loc12_ = _loc12_ - 1;
  298.                _loc9_ = true;
  299.             }
  300.             _loc6_ = _loc6_ + 1;
  301.          }
  302.       }
  303.       if(_loc12_ > 32)
  304.       {
  305.          _loc12_ = 32;
  306.       }
  307.       if(_loc12_ > 0 && this.PLAYER_MOVE == 1)
  308.       {
  309.          _root.EXTRA_UNITS.showExtra = _loc12_;
  310.          _root.EXTRA_UNITS.gotoAndPlay(2);
  311.       }
  312.       this.PLAYERS_CACHES[this.PLAYER_MOVE - 1] = _loc12_;
  313.       var _loc14_ = 0;
  314.       _loc6_ = 0;
  315.       while(_loc6_ < _loc5_.length)
  316.       {
  317.          if(_loc4_[_loc6_] > 0)
  318.          {
  319.             var _loc8_ = this.UNITS_MC.attachMovie("UNITS_ADDER","adder_to_" + _loc5_[_loc6_].unitsMC._name,_loc5_[_loc6_].unitsMC.getDepth() - 1);
  320.             _loc5_[_loc6_].unitsNO += _loc4_[_loc6_];
  321.             _loc8_._x = _loc5_[_loc6_].unitsMC._x + 10;
  322.             _loc8_._y = _loc5_[_loc6_].unitsMC._y;
  323.             _loc8_.unitsMC = _loc5_[_loc6_].unitsMC;
  324.             _loc8_.army = _loc5_[_loc6_].army;
  325.             _loc8_.newUnitsNO = _loc5_[_loc6_].unitsNO;
  326.             _loc8_.player = _loc5_[_loc6_].player;
  327.          }
  328.          _loc6_ = _loc6_ + 1;
  329.       }
  330.       _global.SOUNDS.playSound("przemarsz - do uzupe┼éniania jednostek.wav");
  331.       var _loc13_ = false;
  332.       while(!_loc13_)
  333.       {
  334.          var _loc10_ = this.PLAYER_MOVE + 1;
  335.          if(_loc10_ > this.PLAYERS_NO)
  336.          {
  337.             _loc10_ = 1;
  338.          }
  339.          if(this.countPlayerUnits(this.MAP.TERRITORIES_ARR,_loc10_) > 0)
  340.          {
  341.             _global.setTimeout(mx.utils.Delegate.create(this,this.nextPlayer),2000);
  342.             _loc13_ = true;
  343.          }
  344.          else
  345.          {
  346.             this.PLAYER_MOVE = this.PLAYER_MOVE + 1;
  347.          }
  348.       }
  349.    }
  350.    function unitsToAdd(player)
  351.    {
  352.       var _loc6_ = new Array();
  353.       var _loc2_ = 0;
  354.       while(_loc2_ < this.MAP.TERRITORIES_ARR.length)
  355.       {
  356.          if(this.MAP.TERRITORIES_ARR[_loc2_].player == player)
  357.          {
  358.             _loc6_.push(this.MAP.TERRITORIES_ARR[_loc2_]);
  359.          }
  360.          _loc2_ = _loc2_ + 1;
  361.       }
  362.       var _loc3_ = new Array();
  363.       _loc2_ = 0;
  364.       while(_loc2_ < _loc6_.length)
  365.       {
  366.          var _loc5_ = _loc6_[_loc2_];
  367.          if(this.isThere(_loc5_,_loc3_) == false)
  368.          {
  369.             var _loc4_ = new Array();
  370.             this.generate(_loc5_,_loc4_);
  371.             _loc3_.push(_loc4_);
  372.          }
  373.          _loc2_ = _loc2_ + 1;
  374.       }
  375.       var _loc7_ = 0;
  376.       _loc2_ = 0;
  377.       while(_loc2_ < _loc3_.length)
  378.       {
  379.          if(_loc3_[_loc2_].length > _loc7_)
  380.          {
  381.             _loc7_ = _loc3_[_loc2_].length;
  382.          }
  383.          _loc2_ = _loc2_ + 1;
  384.       }
  385.       return _loc7_;
  386.    }
  387.    function generate(terr, arrToFill)
  388.    {
  389.       if(this.isThere2(terr,arrToFill) == false)
  390.       {
  391.          arrToFill.push(terr);
  392.          var _loc3_ = this.neighboursAreasArr(terr.arr);
  393.          var _loc2_ = 0;
  394.          while(_loc2_ < _loc3_.length)
  395.          {
  396.             this.generate(_loc3_[_loc2_],arrToFill);
  397.             _loc2_ = _loc2_ + 1;
  398.          }
  399.       }
  400.    }
  401.    function isThere2(terr, arr)
  402.    {
  403.       var _loc1_ = 0;
  404.       while(_loc1_ < arr.length)
  405.       {
  406.          if(arr[_loc1_] == terr)
  407.          {
  408.             return true;
  409.          }
  410.          _loc1_ = _loc1_ + 1;
  411.       }
  412.       return false;
  413.    }
  414.    function isThere(terr, arr)
  415.    {
  416.       var _loc2_ = 0;
  417.       while(_loc2_ < arr.length)
  418.       {
  419.          var _loc1_ = 0;
  420.          while(_loc1_ < arr[_loc2_].length)
  421.          {
  422.             if(arr[_loc2_][_loc1_] == terr)
  423.             {
  424.                return true;
  425.             }
  426.             _loc1_ = _loc1_ + 1;
  427.          }
  428.          _loc2_ = _loc2_ + 1;
  429.       }
  430.       return false;
  431.    }
  432.    function neighboursAreasArr(terrArr)
  433.    {
  434.       var _loc6_ = new Array();
  435.       var _loc7_ = 0;
  436.       while(_loc7_ < terrArr.length)
  437.       {
  438.          var _loc4_ = terrArr[_loc7_];
  439.          var _loc3_ = [_loc4_.TL,_loc4_.L,_loc4_.TR,_loc4_.BR,_loc4_.R,_loc4_.BL];
  440.          var _loc1_ = 0;
  441.          while(_loc1_ < _loc3_.length)
  442.          {
  443.             if(_loc3_[_loc1_]._territory != _loc4_._territory && _loc3_[_loc1_]._territory.player == _loc4_._territory.player)
  444.             {
  445.                var _loc5_ = false;
  446.                var _loc2_ = 0;
  447.                while(_loc2_ < _loc6_.length && !_loc5_)
  448.                {
  449.                   if(_loc6_[_loc2_] == _loc3_[_loc1_]._territory)
  450.                   {
  451.                      _loc5_ = true;
  452.                   }
  453.                   _loc2_ = _loc2_ + 1;
  454.                }
  455.                if(!_loc5_)
  456.                {
  457.                   _loc6_.push(_loc3_[_loc1_]._territory);
  458.                }
  459.             }
  460.             _loc1_ = _loc1_ + 1;
  461.          }
  462.          _loc7_ = _loc7_ + 1;
  463.       }
  464.       return _loc6_;
  465.    }
  466.    function fightFinished()
  467.    {
  468.       this.MAP.winnerFound();
  469.       if(this.PLAYER_MOVE == 1)
  470.       {
  471.          this.MAP.selectA();
  472.       }
  473.       else
  474.       {
  475.          var root = this;
  476.          this.TRACER.ct = 0;
  477.          this.TRACER.onEnterFrame = function()
  478.          {
  479.             this.ct = this.ct + 1;
  480.             if(this.ct > 15)
  481.             {
  482.                root.nextAImove();
  483.                delete this.onEnterFrame;
  484.             }
  485.          };
  486.       }
  487.       this.FIGHTBOX.resetWinner();
  488.    }
  489.    function nextAImove(oldID)
  490.    {
  491.       this.AI.cleverMove();
  492.    }
  493.    function kill()
  494.    {
  495.       delete this.TRACER.onEnterFrame;
  496.    }
  497.    function clearAll()
  498.    {
  499.       var _loc2_ = 0;
  500.       while(_loc2_ < this.rubbish_arr.length)
  501.       {
  502.          this.rubbish_arr[_loc2_].removeMovieClip();
  503.          _loc2_ = _loc2_ + 1;
  504.       }
  505.    }
  506.    function random2(z)
  507.    {
  508.       return Math.round(Math.random() * z);
  509.    }
  510.    function random3(z)
  511.    {
  512.       var _loc4_ = new Array();
  513.       var _loc3_ = 0;
  514.       while(_loc3_ <= z)
  515.       {
  516.          var _loc2_ = 0;
  517.          while(_loc2_ <= _loc3_)
  518.          {
  519.             _loc4_.push(_loc3_);
  520.             _loc2_ = _loc2_ + 1;
  521.          }
  522.          _loc3_ = _loc3_ + 1;
  523.       }
  524.       return _loc4_[this.random2(_loc4_.length - 1)];
  525.    }
  526.    function shuffledArray(arr)
  527.    {
  528.       var _loc5_ = 0;
  529.       while(_loc5_ < 20)
  530.       {
  531.          var _loc4_ = this.random2(arr.length - 1);
  532.          var _loc3_ = this.random2(arr.length - 1);
  533.          var _loc6_ = arr[_loc4_];
  534.          arr[_loc4_] = arr[_loc3_];
  535.          arr[_loc3_] = _loc6_;
  536.          _loc5_ = _loc5_ + 1;
  537.       }
  538.       return arr;
  539.    }
  540.    function countPlayerUnits(terrArr, pl)
  541.    {
  542.       var _loc3_ = 0;
  543.       var _loc1_ = 0;
  544.       while(_loc1_ < terrArr.length)
  545.       {
  546.          if(terrArr[_loc1_].player == pl)
  547.          {
  548.             _loc3_ += terrArr[_loc1_].unitsNO;
  549.          }
  550.          _loc1_ = _loc1_ + 1;
  551.       }
  552.       return _loc3_;
  553.    }
  554. }
  555.